<?php
namespace Modules\ShopApi\Http\Middleware;
use Closure;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
use JWTAuth;
class JwtApiAuth
{
    public function handle($request, Closure $next)
    {
		$response = $next($request);
		$api_key = $request->header('apikey');
		if($api_key != config('shopapi.api_key')){
			return response()->json([
                    'code' => 1101,
					'msg'=>'apikey错误！'
                ],401);
		}
		$controller = $request->route()->getAction()['controller'];
		$controller_arr = [
			'Modules\ShopApi\Http\Controllers\v1\AuthController@logout',
			'Modules\ShopApi\Http\Controllers\v1\AuthController@refresh',
			'Modules\ShopApi\Http\Controllers\v1\AuthController@my_collect',
			'Modules\ShopApi\Http\Controllers\v1\AuthController@my_collect_look',
			'Modules\ShopApi\Http\Controllers\v1\AuthController@edit_avatar',
			'Modules\ShopApi\Http\Controllers\v1\AuthController@edit_nick_name',
			'Modules\ShopApi\Http\Controllers\v1\AuthController@config_info',
			
			'Modules\ShopApi\Http\Controllers\v1\IndexController@product_collect',
			'Modules\ShopApi\Http\Controllers\v1\CartController@add_cart',
			'Modules\ShopApi\Http\Controllers\v1\OrderController@add_product_order',
			'Modules\ShopApi\Http\Controllers\v1\OrderController@add_cart_order',
			'Modules\ShopApi\Http\Controllers\v1\OrderController@order_list',
			'Modules\ShopApi\Http\Controllers\v1\SiteController@get_area',
			'Modules\ShopApi\Http\Controllers\v1\SiteController@add_site',
			'Modules\ShopApi\Http\Controllers\v1\SiteController@site_list',
			'Modules\ShopApi\Http\Controllers\v1\SiteController@get_list_info',
			'Modules\ShopApi\Http\Controllers\v1\SiteController@edit_site',
			'Modules\ShopApi\Http\Controllers\v1\RechargeController@get_recharge_list',
		];
		if(!in_array($controller,$controller_arr)){
			return $response;
		} 
		try {
            if (! $user = JWTAuth::parseToken()->authenticate()) {  //获取到用户数据，并赋值给$user
                return response()->json([
                    'code' => 1004,
                    //'msg' => '用户不存在'
					'msg'=>'请先登录！'
                ],400);
            }
			return $response;
 
		} catch (TokenExpiredException $e) {
 
            return response()->json([
                'code' => 1003,
                'msg' => 'token 过期' , //token已过期
            ],400);
 
        } catch (TokenInvalidException $e) {
			if(in_array($controller,[
								'Modules\ShopApi\Http\Controllers\v1\AuthController@logout',
								'Modules\ShopApi\Http\Controllers\v1\AuthController@refresh'
			])){
				return $response;
			}else{
				return response()->json([
					'code' => 1002,
					//'msg' => 'token 无效',  //token无效
					'msg'=>'请先登录！'
				],400);
			} 
        } catch (JWTException $e) {
 
            return response()->json([
                'code' => 1001,
                //'msg' => '缺少token' , //token为空
				'msg'=>'请先登录！'
            ],400);
 
        }
    }
}